
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Search Queries &#8212; PyCantonese 2.2.0 documentation</title>
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Research Outputs" href="papers.html" />
    <link rel="prev" title="Jyutping Romanization: Parsing and Conversion" href="jyutping.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="papers.html" title="Research Outputs"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="jyutping.html" title="Jyutping Romanization: Parsing and Conversion"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyCantonese 2.2.0 documentation</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Search Queries</a><ul>
<li><a class="reference internal" href="#searching-by-a-jyutping-element">Searching by a Jyutping Element</a></li>
<li><a class="reference internal" href="#searching-by-a-chinese-character">Searching by a Chinese Character</a></li>
<li><a class="reference internal" href="#searching-by-a-part-of-speech-tag">Searching by a Part-of-speech Tag</a></li>
<li><a class="reference internal" href="#searching-by-a-word-or-sentence-range">Searching by a Word or Sentence Range</a></li>
<li><a class="reference internal" href="#searching-by-multiple-criteria">Searching by Multiple Criteria</a></li>
<li><a class="reference internal" href="#output-format-of-search-results">Output Format of Search Results</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="jyutping.html"
                        title="previous chapter">Jyutping Romanization: Parsing and Conversion</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="papers.html"
                        title="next chapter">Research Outputs</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="search-queries">
<span id="searches"></span><h1>Search Queries<a class="headerlink" href="#search-queries" title="Permalink to this headline">¶</a></h1>
<p>A common task in corpus-based linguistic work
is to search for specific elements of interest.
PyCantonese provides the general-purpose <code class="docutils literal notranslate"><span class="pre">search()</span></code>
as a corpus object method.
For a given corpus, it can search for specific Jyutping elements,
Chinese characters, part-of-speech tags, and any combinations of these.</p>
<p><code class="docutils literal notranslate"><span class="pre">search()</span></code> is also capable of (so to speak) grabbing the
neighboring words and utterances
around the match word. This is
useful for a wide variety of purposes, e.g., syntax,
semantics, word collocation, discourse analysis, conversation analysis, etc.</p>
<p>All the following examples assume that a corpus object <code class="docutils literal notranslate"><span class="pre">corpus</span></code> for HKCanCor
has been created, and that <code class="docutils literal notranslate"><span class="pre">pprint()</span></code> is used for pretty print.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pycantonese</span> <span class="kn">as</span> <span class="nn">pc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">corpus</span> <span class="o">=</span> <span class="n">pc</span><span class="o">.</span><span class="n">hkcancor</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">corpus</span><span class="o">.</span><span class="n">words</span><span class="p">())</span>  <span class="c1"># total number of words</span>
<span class="go">149781</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">corpus</span><span class="o">.</span><span class="n">characters</span><span class="p">())</span>  <span class="c1"># total number of Chinese characters</span>
<span class="go">186888</span>
</pre></div>
</div>
<p>The following examples show how <code class="docutils literal notranslate"><span class="pre">search()</span></code> works using its array of
parameters.</p>
<ul class="simple">
<li><a class="reference internal" href="#search-jyutping"><span class="std std-ref">Searching by a Jyutping Element</span></a></li>
<li><a class="reference internal" href="#search-character"><span class="std std-ref">Searching by a Chinese Character</span></a></li>
<li><a class="reference internal" href="#search-pos"><span class="std std-ref">Searching by a Part-of-speech Tag</span></a></li>
<li><a class="reference internal" href="#search-range"><span class="std std-ref">Searching by a Word or Sentence Range</span></a></li>
<li><a class="reference internal" href="#search-combination"><span class="std std-ref">Searching by Multiple Criteria</span></a></li>
<li><a class="reference internal" href="#search-format"><span class="std std-ref">Output Format of Search Results</span></a></li>
</ul>
<div class="section" id="searching-by-a-jyutping-element">
<span id="search-jyutping"></span><h2>Searching by a Jyutping Element<a class="headerlink" href="#searching-by-a-jyutping-element" title="Permalink to this headline">¶</a></h2>
<p>Search queries
by various parsed Jyutping elements are possible by passing a parameter
with its value to <code class="docutils literal notranslate"><span class="pre">search()</span></code>.</p>
<p>The Jyutping parameters are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">onset</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">nucleus</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">coda</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">tone</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">initial</span></code> (聲母, equivalent to <code class="docutils literal notranslate"><span class="pre">onset</span></code>)</li>
<li><code class="docutils literal notranslate"><span class="pre">final</span></code> (韻母, equivalent to nucleus+coda)</li>
<li><code class="docutils literal notranslate"><span class="pre">jyutping</span></code> (a complete Jyutping romanization, onset+nucleus+coda+tone)</li>
</ul>
<p>For example, for the nucleus “aa”:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">aa</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">nucleus</span><span class="o">=</span><span class="s1">&#39;aa&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">aa</span><span class="p">)</span>  <span class="c1"># number of matching results found</span>
<span class="go">21830</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">aa</span><span class="p">[:</span> <span class="mi">5</span><span class="p">])</span>  <span class="c1"># show first 5 results</span>
<span class="go">[(&#39;啊&#39;, &#39;Y&#39;, &#39;aa3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;啊&#39;, &#39;Y&#39;, &#39;aa3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;淡季&#39;, &#39;AN&#39;, &#39;daam6gwai3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;𡃉&#39;, &#39;Y&#39;, &#39;gaa3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;嗱&#39;, &#39;Y&#39;, &#39;laa4&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">tone</span></code> parameter takes a str but not an int:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tone2</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">tone</span><span class="o">=</span><span class="s1">&#39;2&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">tone2</span><span class="p">)</span>
<span class="go">20579</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">tone2</span><span class="p">[:</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">[(&#39;講&#39;, &#39;V&#39;, &#39;gong2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;嗰個&#39;, &#39;R&#39;, &#39;go2go3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;嗰個&#39;, &#39;R&#39;, &#39;go2go3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;好&#39;, &#39;D&#39;, &#39;hou2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;抵&#39;, &#39;A&#39;, &#39;dai2&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>The parameters <code class="docutils literal notranslate"><span class="pre">onset</span></code>, <code class="docutils literal notranslate"><span class="pre">nucleus</span></code>, <code class="docutils literal notranslate"><span class="pre">coda</span></code>, <code class="docutils literal notranslate"><span class="pre">tone</span></code>, and <code class="docutils literal notranslate"><span class="pre">initial</span></code>
may take a regular expression for more powerful search queries.
For instance, we may ask for all words that contain any of the codas {p, t, k}.
<code class="docutils literal notranslate"><span class="pre">[ptk]</span></code> can be used for regular expression matching for any of these letters,
and we set it to be the value of the <code class="docutils literal notranslate"><span class="pre">coda</span></code> parameter:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">codas_ptk</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">coda</span><span class="o">=</span><span class="s1">&#39;[ptk]&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">codas_ptk</span><span class="p">)</span>
<span class="go">12409</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">codas_ptk</span><span class="p">[:</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">[(&#39;迪士尼&#39;, &#39;NT&#39;, &#39;dik6si6nei4&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;直程&#39;, &#39;D&#39;, &#39;zik6cing4&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;七&#39;, &#39;M&#39;, &#39;cat1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;八月&#39;, &#39;T&#39;, &#39;baat3jyut6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;日&#39;, &#39;Q&#39;, &#39;jat6&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>For regular expressions in Python, see
<a class="reference external" href="https://docs.python.org/3/library/re.html">here</a>.</p>
<p>The search criteria can be mixed in a single <code class="docutils literal notranslate"><span class="pre">search()</span></code> call.
However, in order to avoid possible conflicts,
restrictions are imposed on the combination of Jyutping-related search criteria:</p>
<ul class="simple">
<li>If <code class="docutils literal notranslate"><span class="pre">jyutping</span></code> is used, no other Jyutping search parameters can be used.</li>
<li>If <code class="docutils literal notranslate"><span class="pre">final</span></code> is used, neither <code class="docutils literal notranslate"><span class="pre">nucleus</span></code> nor <code class="docutils literal notranslate"><span class="pre">coda</span></code> can be used.</li>
</ul>
</div>
<div class="section" id="searching-by-a-chinese-character">
<span id="search-character"></span><h2>Searching by a Chinese Character<a class="headerlink" href="#searching-by-a-chinese-character" title="Permalink to this headline">¶</a></h2>
<p>Search queries for a given Chinese character are performed by the <code class="docutils literal notranslate"><span class="pre">character</span></code>
parameter:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">machine</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">character</span><span class="o">=</span><span class="s1">&#39;機&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">machine</span><span class="p">)</span>
<span class="go">184</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">machine</span><span class="p">[:</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">[(&#39;機票&#39;, &#39;N&#39;, &#39;gei1piu3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;機票&#39;, &#39;N&#39;, &#39;gei1piu3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;機票&#39;, &#39;N&#39;, &#39;gei1piu3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;飛機&#39;, &#39;N&#39;, &#39;fei1gei1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;機&#39;, &#39;NG&#39;, &#39;gei1&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>If you are using Python 2, use <code class="docutils literal notranslate"><span class="pre">corpus.search(character=u'機')</span></code> (note the “u”
prefix for the string literal) for unicode.</p>
</div>
<div class="section" id="searching-by-a-part-of-speech-tag">
<span id="search-pos"></span><h2>Searching by a Part-of-speech Tag<a class="headerlink" href="#searching-by-a-part-of-speech-tag" title="Permalink to this headline">¶</a></h2>
<p>With the parameter <code class="docutils literal notranslate"><span class="pre">pos</span></code> in <code class="docutils literal notranslate"><span class="pre">search()</span></code>,
verbs which bear the part-of-speech tag “V” in HKCanCor
can be accessed as follows:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">verbs</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="s1">&#39;V&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">verbs</span><span class="p">)</span>
<span class="go">23768</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">verbs</span><span class="p">[:</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">[(&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">pos</span></code> parameter may take a regular expression. For instance,
we can use <code class="docutils literal notranslate"><span class="pre">'^V'</span></code> to match any part-of-speech tags that begin with “V” for
different kinds of verbs annotated in HKCanCor:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">all_verbs</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="s1">&#39;^V&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">all_verbs</span><span class="p">)</span>  <span class="c1"># number of all verbs -- more than just &quot;V&quot; alone above</span>
<span class="go">29012</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">all_verbs</span><span class="p">[:</span><span class="mi">20</span><span class="p">])</span>  <span class="c1"># printing the first 20 results</span>
<span class="go">[(&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;旅行&#39;, &#39;VN&#39;, &#39;leoi5hang4&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;有冇&#39;, &#39;V1&#39;, &#39;jau5mou5&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;要&#39;, &#39;VU&#39;, &#39;jiu3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;有得&#39;, &#39;VU&#39;, &#39;jau5dak1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;冇得&#39;, &#39;VU&#39;, &#39;mou5dak1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;聽&#39;, &#39;V&#39;, &#39;teng1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;講&#39;, &#39;V&#39;, &#39;gong2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;話&#39;, &#39;V&#39;, &#39;waa6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;玩&#39;, &#39;V&#39;, &#39;waan2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;可以&#39;, &#39;VU&#39;, &#39;ho2ji5&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;住&#39;, &#39;V&#39;, &#39;zyu6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;話&#39;, &#39;V&#39;, &#39;waa6&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;跟&#39;, &#39;V&#39;, &#39;gan1&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
<p>For regular expressions in Python, see
<a class="reference external" href="https://docs.python.org/3/library/re.html">here</a>.</p>
<p>For the part-of-speech tagset used by HKCanCor, see <a class="reference external" href="http://compling.hss.ntu.edu.sg/hkcancor/">here</a>.</p>
</div>
<div class="section" id="searching-by-a-word-or-sentence-range">
<span id="search-range"></span><h2>Searching by a Word or Sentence Range<a class="headerlink" href="#searching-by-a-word-or-sentence-range" title="Permalink to this headline">¶</a></h2>
<p>It is possible to include in search results the neighboring words and sentences
with respect to a match word. This functionality is useful for syntax,
semantics, and discourse-level research.</p>
<p>The parameters <code class="docutils literal notranslate"><span class="pre">word_range</span></code> and <code class="docutils literal notranslate"><span class="pre">sent_range</span></code> each take a tuple of
(int, int).</p>
<p><code class="docutils literal notranslate"><span class="pre">word_range</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code> for 0 <em>words</em>
(the first <code class="docutils literal notranslate"><span class="pre">0</span></code>)
on the left of a match word and 0 <em>words</em> (the second <code class="docutils literal notranslate"><span class="pre">0</span></code>)
on the right – all within the same sent (= sentence).
Likewise, <code class="docutils literal notranslate"><span class="pre">sent_range</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code> for 0 <em>sents</em>
preceding the sent containing the match word and 0 <em>sents</em> following it.</p>
<p>For <code class="docutils literal notranslate"><span class="pre">word_range</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gwo3</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">character</span><span class="o">=</span><span class="s1">&#39;過&#39;</span><span class="p">,</span> <span class="n">word_range</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>  <span class="c1"># use u&#39;過&#39; instead in python 2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">gwo3</span><span class="p">)</span>
<span class="go">679</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">gwo3</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>
<span class="go">[[(&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;過&#39;, &#39;U&#39;, &#39;gwo3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;喇&#39;, &#39;Y&#39;, &#39;laa1&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;.&#39;, &#39;.&#39;, &#39;&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;不過&#39;, &#39;C&#39;, &#39;bat1gwo3&#39;, &#39;&#39;), (&#39;幾&#39;, &#39;M&#39;, &#39;gei2&#39;, &#39;&#39;), (&#39;日&#39;, &#39;Q&#39;, &#39;jat6&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;去&#39;, &#39;VK&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;過&#39;, &#39;V&#39;, &#39;gwo3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;嗰邊&#39;, &#39;R&#39;, &#39;go2bin1&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;瞓覺&#39;, &#39;V&#39;, &#39;fan3gaau3&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;不過&#39;, &#39;C&#39;, &#39;bat1gwo3&#39;, &#39;&#39;), (&#39;都&#39;, &#39;D&#39;, &#39;dou1&#39;, &#39;&#39;), (&#39;,&#39;, &#39;,&#39;, &#39;&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;,&#39;, &#39;,&#39;, &#39;&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;不過&#39;, &#39;C&#39;, &#39;bat1gwo3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;真係&#39;, &#39;D&#39;, &#39;zan1hai6&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;好&#39;, &#39;D&#39;, &#39;hou2&#39;, &#39;&#39;)]]</span>
</pre></div>
</div>
<p>Note that the return object is list(list(tagged words)) when <code class="docutils literal notranslate"><span class="pre">word_range</span></code>
is used. Also, the words that <code class="docutils literal notranslate"><span class="pre">word_range</span></code> specifies do not
cross sentence boundaries.</p>
<p>For <code class="docutils literal notranslate"><span class="pre">sent_range</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">laa1</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">jyutping</span><span class="o">=</span><span class="s1">&#39;laa1&#39;</span><span class="p">,</span> <span class="n">sent_range</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">laa1</span><span class="p">)</span>
<span class="go">1583</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">laa1</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>  <span class="c1"># print the 1st result</span>
<span class="go">[[(&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;唔係&#39;, &#39;V&#39;, &#39;m4hai6&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;啊&#39;, &#39;Y&#39;, &#39;aa3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;?&#39;, &#39;?&#39;, &#39;&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;你&#39;, &#39;R&#39;, &#39;nei5&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;都&#39;, &#39;D&#39;, &#39;dou1&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;去&#39;, &#39;V&#39;, &#39;heoi3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;過&#39;, &#39;U&#39;, &#39;gwo3&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;喇&#39;, &#39;Y&#39;, &#39;laa1&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;.&#39;, &#39;.&#39;, &#39;&#39;, &#39;&#39;)],</span>
<span class="go"> [(&#39;咪&#39;, &#39;C&#39;, &#39;mai6&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;係&#39;, &#39;V&#39;, &#39;hai6&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;囖&#39;, &#39;Y&#39;, &#39;lo1&#39;, &#39;&#39;),</span>
<span class="go">  (&#39;.&#39;, &#39;.&#39;, &#39;&#39;, &#39;&#39;)]]</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">sent_range</span></code> is not <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code>, <code class="docutils literal notranslate"><span class="pre">word_range</span></code> is ignored (as full
sentences are in the output anyway).</p>
</div>
<div class="section" id="searching-by-multiple-criteria">
<span id="search-combination"></span><h2>Searching by Multiple Criteria<a class="headerlink" href="#searching-by-multiple-criteria" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">search()</span></code> is flexible and allows multiple parameters described
above to be specified at the same time.
For instance, if we are interested in <em>pinjam</em> (“tone change”) in Cantonese,
we may be interested in all words with coda {p, t, k} plus tone 2 (high-rising):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ptk_tone2</span> <span class="o">=</span> <span class="n">corpus</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">coda</span><span class="o">=</span><span class="s1">&#39;[ptk]&#39;</span><span class="p">,</span> <span class="n">tone</span><span class="o">=</span><span class="s1">&#39;2&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">ptk_tone2</span><span class="p">)</span>
<span class="go">70</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">ptk_tone2</span><span class="p">[:</span> <span class="mi">10</span><span class="p">])</span>
<span class="go">[(&#39;雀&#39;, &#39;N&#39;, &#39;zoek2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;雀&#39;, &#39;N&#39;, &#39;zoek2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;綠&#39;, &#39;A&#39;, &#39;luk2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;dut2&#39;, &#39;O&#39;, &#39;dut2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;碟&#39;, &#39;N&#39;, &#39;dip2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;碟&#39;, &#39;N&#39;, &#39;dip2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;碟&#39;, &#39;N&#39;, &#39;dip2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;碟形&#39;, &#39;N&#39;, &#39;dip2jing4&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;碟&#39;, &#39;N&#39;, &#39;dip2&#39;, &#39;&#39;),</span>
<span class="go"> (&#39;soek2&#39;, &#39;O&#39;, &#39;soek2&#39;, &#39;&#39;)]</span>
</pre></div>
</div>
</div>
<div class="section" id="output-format-of-search-results">
<span id="search-format"></span><h2>Output Format of Search Results<a class="headerlink" href="#output-format-of-search-results" title="Permalink to this headline">¶</a></h2>
<p>While <code class="docutils literal notranslate"><span class="pre">search()</span></code> always returns a list, the format of the elements in the list
can be adjusted by the parameters <code class="docutils literal notranslate"><span class="pre">tagged</span></code> and <code class="docutils literal notranslate"><span class="pre">sents</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">tagged</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (default), words are all represented in the “tagged”
format of (word, part-of-speech tag, Jyutping, rel),
as in all the examples above. Otherwise, words are word token strings with
Chinese characters only.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">sents</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (default), the elements in the output list are words
(or spans of words when <code class="docutils literal notranslate"><span class="pre">word_range</span></code> is used). Otherwise, all sents
containing a match word are in the output list. If <code class="docutils literal notranslate"><span class="pre">sent_range</span></code> is used,
<code class="docutils literal notranslate"><span class="pre">sents</span></code> is automatically <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="papers.html" title="Research Outputs"
             >next</a> |</li>
        <li class="right" >
          <a href="jyutping.html" title="Jyutping Romanization: Parsing and Conversion"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyCantonese 2.2.0 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2014-2018, Jackson L. Lee | Documentation last updated on June 30, 2018.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.5.
    </div>
  </body>
</html>